package com.lzc.tlmail.tlmailorder.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzc
 * @create 2020-10-22 19:41
 */
@Configuration
public class MyMQConfig {

    /**
     * 这里放信死了的交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange("order-event-exchange",true,false);
    }

    /**
     *普通队列
     * @return
     */
    @Bean
    public Queue  orderReleaseOrderQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, @Nullable Map<String, Object> arguments
        return new Queue("order.release.order.queue",true,false,false);
    }

    /**
     *秒杀队列
     * @return
     */
    @Bean
    public Queue  orderSeckillOrderQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, @Nullable Map<String, Object> arguments
        return new Queue("order.seckill.order.queue",true,false,false);
    }

    /**
     * 延时队列  死信队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        /**
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 1800000
         *
         */
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange","order-event-exchange");
        args.put("x-dead-letter-routing-key","order.release");
        args.put("x-message-ttl",1800000);
        //String name, boolean durable, boolean exclusive, boolean autoDelete, @Nullable Map<String, Object> arguments
        return new Queue("order.delay.queue",true,false,false,args);
    }

    /**
     * 绑定关系 跟延时队列绑定
     * @return
     */
    @Bean
    public Binding orderCreateOrderBingding(){
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }


    /**
     * 绑定关系 跟普通队列绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBingding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.#",
                null);
    }

    /**
     * 绑定关系 跟库存释放队列绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    /**
     * 绑定关系 跟秒杀队列绑定
     * @return
     */
    @Bean
    public Binding orderSeckillOrderBingding(){
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }
}
